<?php
/**
 * Created by IntelliJ IDEA.
 * User: luo
 * Date: 18-3-16
 * Time: 上午9:17
 */

namespace app\tools;

use Yii;
use yii\log\FileTarget;

class Tool
{
    /**
     * @param $message
     * @param string $target
     * @throws \yii\base\InvalidConfigException
     * @throws \yii\log\LogRuntimeException
     */
    public static function log($message, $target = 'app.log'){
        if(is_array($message) or is_object($message)){
            $message = var_export($message, true);
        }

        $log = new FileTarget();
        $log->logFile = Yii::$app->getRuntimePath() . '/logs/'.$target;
        $log->messages[] = [
            $message,
            1,
            'application',
            time()
        ];
        $log->export();
    }
    
    public static function str_replaces($str, $start, $len, $symbol='*') {
        $end = $start + $len;
        // 截取头保留的字符
        $str1 = mb_substr($str, 0, $start);
        // 截取尾保留的字符
        $str2 = mb_substr($str, $end);

        //  生产要替换的字符
        $symbol_num = '';
        for ($i = 0; $i < $len; $i++) {
            $symbol_num .= $symbol;
        }
        return $str1 . $symbol_num . $str2;
    }

    public static function getSn($str) {
        return $str.date('Ymd', time()).rand(0,9999);
    }
}
